package com.demo.hc.controller.area;

import com.demo.hc.biz.area.bean.AreaTypeEnum;
import com.demo.hc.biz.area.service.AreaService;
import com.demo.hc.common.web.JsonResult;
import com.demo.hc.controller.base.BaseController;
import com.demo.hc.model.mbg.Area;
import com.demo.hc.model.view.AreaView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Description("区域控制器")
@Controller
@RequestMapping("area")
public class AreaController extends BaseController {

    @Autowired
    private AreaService areaService;

    @Description("区域信息跳转页面")
    @RequestMapping(value = "index",method= RequestMethod.GET)
    public String index(HttpServletRequest request, HttpServletResponse response){
        request.setAttribute("areaType",2);
        return "area/index";
    }

    @Description("区域详细信息")
    @RequestMapping(value = "detail",method= RequestMethod.GET)
    public String detail(HttpServletRequest request, HttpServletResponse response, int areaId){
        Area area = areaService.getAreaById(areaId);
        request.setAttribute("area", area);
        return "area/detail";
    }

    @Description("区域列表信息")
    @RequestMapping(value = "list",method= RequestMethod.GET)
    @ResponseBody
    public JsonResult list(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam(value = "areaType",required = false) Integer areaType,
                           @RequestParam(value = "parentId",required = false) Integer parentId){
        AreaTypeEnum typeEnum = null;
        if(areaType != null){
            typeEnum = AreaTypeEnum.getAreaTypeEnum(areaType);
        }
        List<AreaView> areaViewList = areaService.listArea(typeEnum,parentId);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("areaViewList",areaViewList);
        return JsonResult.getResult(resultMap);
    }


}
